<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>module Observable - observer: Ruby Standard Library Documentation</title>


<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>

<script src="./js/jquery-3.2.0.min.js"></script>

<script src="./js/vue.min.js"></script>
<script src="./js/js.cookie.min.js"></script>

<link href="./css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="./css/rdoc.css" rel="stylesheet">
<link href="./css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "./";
  var index_rel_prefix = "./";
  var darkModeCsseHref = "./css/rdoc-dm.css"
  var defaultModeCssHref = "./css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="module">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0.preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="./index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="./table_of_contents.html#pages">Pages</a>
    <a href="./table_of_contents.html#classes">Classes</a>
    <a href="./table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#module-Observable-label-Mechanism">Mechanism</a>
    <li><a href="#module-Observable-label-Example">Example</a>
    <li><a href="#module-Observable-label-Usage+with+procs">Usage with procs</a>
  </ul>
</div>


    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
        
        
        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-i-add_observer">#add_observer</a>
    <li ><a href="#method-i-changed">#changed</a>
    <li ><a href="#method-i-changed-3F">#changed?</a>
    <li ><a href="#method-i-count_observers">#count_observers</a>
    <li ><a href="#method-i-delete_observer">#delete_observer</a>
    <li ><a href="#method-i-delete_observers">#delete_observers</a>
    <li ><a href="#method-i-notify_observers">#notify_observers</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="module-Observable">
    <h1 id="module-Observable" class="module">
      module Observable
    </h1>

    <section class="description">
    
<p>The Observer pattern (also known as publish/subscribe) provides a simple mechanism for one object to inform a set of interested third-party objects when its state changes.</p>

<h2 id="module-Observable-label-Mechanism">Mechanism<span><a href="#module-Observable-label-Mechanism">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>The notifying class mixes in the <code>Observable</code> module, which provides the methods for managing the associated observer objects.</p>

<p>The observable object must:</p>
<ul><li>
<p>assert that it has <code>#changed</code></p>
</li><li>
<p>call <code>#notify_observers</code></p>
</li></ul>

<p>An observer subscribes to updates using <a href="Observable.html#method-i-add_observer"><code>Observable#add_observer</code></a>, which also specifies the method called via <a href="Observable.html#method-i-notify_observers"><code>notify_observers</code></a>. The default method for <a href="Observable.html#method-i-notify_observers"><code>notify_observers</code></a> is update.</p>

<h3 id="module-Observable-label-Example">Example<span><a href="#module-Observable-label-Example">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>The following example demonstrates this nicely.  A <code>Ticker</code>, when run, continually receives the stock <code>Price</code> for its <code>@symbol</code>.  A <code>Warner</code> is a general observer of the price, and two warners are demonstrated, a <code>WarnLow</code> and a <code>WarnHigh</code>, which print a warning if the price is below or above their set limits, respectively.</p>

<p>The <code>update</code> callback allows the warners to run without being explicitly called.  The system is set up with the <code>Ticker</code> and several observers, and the observers do their duty without the top-level code having to interfere.</p>

<p>Note that the contract between publisher and subscriber (observable and observer) is not declared or enforced.  The <code>Ticker</code> publishes a time and a price, and the warners receive that.  But if you don’t ensure that your contracts are correct, nothing else can warn you.</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&quot;observer&quot;</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">Ticker</span>          <span class="ruby-comment">### Periodically fetch a stock price.</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">Observable</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-identifier">symbol</span>)
    <span class="ruby-ivar">@symbol</span> = <span class="ruby-identifier">symbol</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">run</span>
    <span class="ruby-identifier">last_price</span> = <span class="ruby-keyword">nil</span>
    <span class="ruby-identifier">loop</span> <span class="ruby-keyword">do</span>
      <span class="ruby-identifier">price</span> = <span class="ruby-constant">Price</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-ivar">@symbol</span>)
      <span class="ruby-identifier">print</span> <span class="ruby-node">&quot;Current price: #{price}\n&quot;</span>
      <span class="ruby-keyword">if</span> <span class="ruby-identifier">price</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">last_price</span>
        <span class="ruby-identifier">changed</span>                 <span class="ruby-comment"># notify observers</span>
        <span class="ruby-identifier">last_price</span> = <span class="ruby-identifier">price</span>
        <span class="ruby-identifier">notify_observers</span>(<span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>, <span class="ruby-identifier">price</span>)
      <span class="ruby-keyword">end</span>
      <span class="ruby-identifier">sleep</span> <span class="ruby-value">1</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">Price</span>           <span class="ruby-comment">### A mock class to fetch a stock price (60 - 140).</span>
  <span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">fetch</span>(<span class="ruby-identifier">symbol</span>)
    <span class="ruby-value">60</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">rand</span>(<span class="ruby-value">80</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">Warner</span>          <span class="ruby-comment">### An abstract observer of Ticker objects.</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-identifier">ticker</span>, <span class="ruby-identifier">limit</span>)
    <span class="ruby-ivar">@limit</span> = <span class="ruby-identifier">limit</span>
    <span class="ruby-identifier">ticker</span>.<span class="ruby-identifier">add_observer</span>(<span class="ruby-keyword">self</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">WarnLow</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">Warner</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">update</span>(<span class="ruby-identifier">time</span>, <span class="ruby-identifier">price</span>)       <span class="ruby-comment"># callback for observer</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">price</span> <span class="ruby-operator">&lt;</span> <span class="ruby-ivar">@limit</span>
      <span class="ruby-identifier">print</span> <span class="ruby-node">&quot;--- #{time.to_s}: Price below #@limit: #{price}\n&quot;</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">WarnHigh</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">Warner</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">update</span>(<span class="ruby-identifier">time</span>, <span class="ruby-identifier">price</span>)       <span class="ruby-comment"># callback for observer</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">price</span> <span class="ruby-operator">&gt;</span> <span class="ruby-ivar">@limit</span>
      <span class="ruby-identifier">print</span> <span class="ruby-node">&quot;+++ #{time.to_s}: Price above #@limit: #{price}\n&quot;</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">ticker</span> = <span class="ruby-constant">Ticker</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&quot;MSFT&quot;</span>)
<span class="ruby-constant">WarnLow</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">ticker</span>, <span class="ruby-value">80</span>)
<span class="ruby-constant">WarnHigh</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">ticker</span>, <span class="ruby-value">120</span>)
<span class="ruby-identifier">ticker</span>.<span class="ruby-identifier">run</span>
</pre>

<p>Produces:</p>

<pre>Current price: 83
Current price: 75
--- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 75
Current price: 90
Current price: 134
+++ Sun Jun 09 00:10:25 CDT 2002: Price above 120: 134
Current price: 134
Current price: 112
Current price: 79
--- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 79</pre>

<h3 id="module-Observable-label-Usage+with+procs">Usage with procs<span><a href="#module-Observable-label-Usage+with+procs">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>The <code>#notify_observers</code> method can also be used with +proc+s by using the <code>:call</code> as <code>func</code> parameter.</p>

<p>The following example illustrates the use of a lambda:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;observer&#39;</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">Ticker</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">Observable</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">run</span>
    <span class="ruby-comment"># logic to retrieve the price (here 77.0)</span>
    <span class="ruby-identifier">changed</span>
    <span class="ruby-identifier">notify_observers</span>(<span class="ruby-value">77.0</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">ticker</span> = <span class="ruby-constant">Ticker</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">warner</span> = <span class="ruby-operator">-&gt;</span>(<span class="ruby-identifier">price</span>) { <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;New price received: #{price}&quot;</span> }
<span class="ruby-identifier">ticker</span>.<span class="ruby-identifier">add_observer</span>(<span class="ruby-identifier">warner</span>, <span class="ruby-value">:call</span>)
<span class="ruby-identifier">ticker</span>.<span class="ruby-identifier">run</span>
</pre>

    </section>

      <section id="5Buntitled-5D" class="documentation-section">


      <section class="constants-list">
      <header>
      <h3>Constants</h3>
      </header>
      <dl>
          <dt id="VERSION">VERSION
          <dd>
        </dl>
        </section>



                <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Instance Methods</h3>
                </header>

                  <div id="method-i-add_observer" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">add_observer</span><span
                                class="method-args">(observer, func=:update)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Add <code>observer</code> as an observer on this object. So that it will receive notifications.</p>
<dl class="rdoc-list note-list"><dt><code>observer</code>
<dd>
<p>the object that will be notified of changes.</p>
</dd><dt><code>func</code>
<dd>
<p>Symbol naming the method that will be called when this <a href="Observable.html"><code>Observable</code></a> has changes.</p>

<p>This method must return true for <code>observer.respond_to?</code> and will receive <code>*arg</code> when <a href="Observable.html#method-i-notify_observers"><code>notify_observers</code></a> is called, where <code>*arg</code> is the value passed to <a href="Observable.html#method-i-notify_observers"><code>notify_observers</code></a> by this <a href="Observable.html"><code>Observable</code></a></p>
</dd></dl>

                              <div class="method-source-code" id="add_observer-source">
            <pre><span class="ruby-comment"># File observer.rb, line 153</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add_observer</span>(<span class="ruby-identifier">observer</span>, <span class="ruby-identifier">func</span>=<span class="ruby-value">:update</span>)
  <span class="ruby-ivar">@observer_peers</span> = {} <span class="ruby-keyword">unless</span> <span class="ruby-keyword">defined?</span> <span class="ruby-ivar">@observer_peers</span>
  <span class="ruby-keyword">unless</span> <span class="ruby-identifier">observer</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-identifier">func</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-constant">NoMethodError</span>, <span class="ruby-node">&quot;observer does not respond to `#{func}&#39;&quot;</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-ivar">@observer_peers</span>[<span class="ruby-identifier">observer</span>] = <span class="ruby-identifier">func</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-changed" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">changed</span><span
                                class="method-args">(state=true)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Set the changed state of this object.  Notifications will be sent only if the changed <code>state</code> is <code>true</code>.</p>
<dl class="rdoc-list note-list"><dt><code>state</code>
<dd>
<p>Boolean indicating the changed state of this <a href="Observable.html"><code>Observable</code></a>.</p>
</dd></dl>

                              <div class="method-source-code" id="changed-source">
            <pre><span class="ruby-comment"># File observer.rb, line 194</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">changed</span>(<span class="ruby-identifier">state</span>=<span class="ruby-keyword">true</span>)
  <span class="ruby-ivar">@observer_state</span> = <span class="ruby-identifier">state</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-changed-3F" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">changed?</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns true if this object’s state has been changed since the last <a href="Observable.html#method-i-notify_observers"><code>notify_observers</code></a> call.</p>

                              <div class="method-source-code" id="changed-3F-source">
            <pre><span class="ruby-comment"># File observer.rb, line 202</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">changed?</span>
  <span class="ruby-keyword">if</span> <span class="ruby-keyword">defined?</span> <span class="ruby-ivar">@observer_state</span> <span class="ruby-keyword">and</span> <span class="ruby-ivar">@observer_state</span>
    <span class="ruby-keyword">true</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-keyword">false</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-count_observers" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">count_observers</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Return the number of observers associated with this object.</p>

                              <div class="method-source-code" id="count_observers-source">
            <pre><span class="ruby-comment"># File observer.rb, line 180</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">count_observers</span>
  <span class="ruby-keyword">if</span> <span class="ruby-keyword">defined?</span> <span class="ruby-ivar">@observer_peers</span>
    <span class="ruby-ivar">@observer_peers</span>.<span class="ruby-identifier">size</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-value">0</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-delete_observer" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">delete_observer</span><span
                                class="method-args">(observer)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Remove <code>observer</code> as an observer on this object so that it will no longer receive notifications.</p>
<dl class="rdoc-list note-list"><dt><code>observer</code>
<dd>
<p>An observer of this <a href="Observable.html"><code>Observable</code></a></p>
</dd></dl>

                              <div class="method-source-code" id="delete_observer-source">
            <pre><span class="ruby-comment"># File observer.rb, line 166</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">delete_observer</span>(<span class="ruby-identifier">observer</span>)
  <span class="ruby-ivar">@observer_peers</span>.<span class="ruby-identifier">delete</span> <span class="ruby-identifier">observer</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">defined?</span> <span class="ruby-ivar">@observer_peers</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-delete_observers" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">delete_observers</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Remove all observers associated with this object.</p>

                              <div class="method-source-code" id="delete_observers-source">
            <pre><span class="ruby-comment"># File observer.rb, line 173</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">delete_observers</span>
  <span class="ruby-ivar">@observer_peers</span>.<span class="ruby-identifier">clear</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">defined?</span> <span class="ruby-ivar">@observer_peers</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-notify_observers" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">notify_observers</span><span
                                class="method-args">(*arg)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Notify observers of a change in state <strong>if</strong> this object’s changed state is <code>true</code>.</p>

<p>This will invoke the method named in <a href="Observable.html#method-i-add_observer"><code>add_observer</code></a>, passing <code>*arg</code>. The changed state is then set to <code>false</code>.</p>
<dl class="rdoc-list note-list"><dt><code>*arg</code>
<dd>
<p>Any arguments to pass to the observers.</p>
</dd></dl>

                              <div class="method-source-code" id="notify_observers-source">
            <pre><span class="ruby-comment"># File observer.rb, line 218</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">notify_observers</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">arg</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-keyword">defined?</span> <span class="ruby-ivar">@observer_state</span> <span class="ruby-keyword">and</span> <span class="ruby-ivar">@observer_state</span>
    <span class="ruby-keyword">if</span> <span class="ruby-keyword">defined?</span> <span class="ruby-ivar">@observer_peers</span>
      <span class="ruby-ivar">@observer_peers</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">k</span>, <span class="ruby-identifier">v</span><span class="ruby-operator">|</span>
        <span class="ruby-identifier">k</span>.<span class="ruby-identifier">__send__</span>(<span class="ruby-identifier">v</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">arg</span>)
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-ivar">@observer_state</span> = <span class="ruby-keyword">false</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is a service of <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>, purveyors of fine <a href='https://jamesbritt.bandcamp.com/'>dance noise</a></p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

